// 文件名称: StudentService.java
// 创建日期: 2023-10-05
// 描述: 学生服务类，提供获取随机学生和随机小组的方法。

package org.wisdom.studentchoose.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import org.wisdom.studentchoose.db.Student;

import static org.wisdom.studentchoose.db.DatabaseConnection.getConnection;

/**
 * 学生服务类，提供获取随机学生和随机小组的方法。
 */
public class StudentService {

    /**
     * 获取随机学生。
     *
     * @return 随机选择的学生对象，如果没有学生则返回 null
     */
    public static Student getRandomStudent() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Student student = null;

        try {
            conn = getConnection();
            // 修改后的查询语句，包含所有需要的字段
            String sql = "SELECT id, name, major, class_name AS className, group_name AS groupName, student_number AS number, grade, gender FROM students ORDER BY RAND() LIMIT 1";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();

            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String major = rs.getString("major");
                String className = rs.getString("className");
                String groupName = rs.getString("groupName");
                String number = rs.getString("number");
                double grade = rs.getDouble("grade");
                String gender = rs.getString("gender");

                // 使用包含所有字段的构造函数
                student = new Student(id, name, major, className, groupName, number, grade, gender);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return student;
    }

    /**
     * 根据小组名称获取随机学生。
     *
     * @param group 小组名称
     * @return 随机选择的学生对象，如果没有学生则返回 null
     */
    public static Student getRandomStudentByGroup(String group) {
        // 使用 DAO 查询指定小组的学生
        StudentDAO studentDAO = new StudentDAO();
        List<Student> students = studentDAO.getStudentsByGroup(group);

        if (students.isEmpty()) {
            return null;
        }

        // 随机选择一个学生
        int randomIndex = new Random().nextInt(students.size());
        return students.get(randomIndex);
    }

    /**
     * 获取随机小组。
     *
     * @return 随机选择的小组名称，如果没有小组则返回 null
     */
    public static String getRandomGroup() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String group = null;

        try {
            conn = getConnection();
            // 随机选择一个小组
            String sql = "SELECT name FROM `groups` ORDER BY RAND() LIMIT 1";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();

            if (rs.next()) {
                group = rs.getString("name");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return group;
    }
}
